<?php
namespace backend\controller;
use backend\controller\BackendBaseController;
use backend\model\role_auth;
use backend\model\admin_model;
use jimu\Request;

class AdminController extends BackendBaseController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function indexAction()
    {



    }

    /**
     * 管理员登录
     */
    public function loginAction()
    {
        if(!Request::post()){
            display();
            exit;
        }
        $vars = Request::post();

        if(empty($vars['code'])){
            ajaxReturn(['msg'=>'验证码不能为空','code'=>-1]);
        }
        if( strtolower($vars['code']) !=  $_SESSION['code'] ){
            $_SESSION['code'] = '';
            ajaxReturn(['msg'=>'验证码错误','code'=>-5]);
        }
//        $vars = ['username'=>'william','password'=>'admin'];

        try{
            $admin_model = new admin_model();
            $admin_model->useConnect('slave');
            $admin_model->login($vars);

            $admin = $admin_model->getOneByName($vars['username']);
            $_SESSION['admin'] = $admin;
            ajaxReturn(['msg'=>'success','code'=>1]);
        }catch(\Exception $e){
            ajaxReturn(['msg'=>$e->getMessage(),'code'=>$e->getCode()]);
        }
    }

    public function logoutAction()
    {
        if(isset($_SESSION['admin']))
        {
            unset($_SESSION['admin']);
        }
        redirect('/backend/admin/login');
    }

    public function profileAction()
    {
        return '大兄弟，功能还没实现';
    }

    public function editpwdAction()
    {
        return '大兄弟，功能还没实现';
    }


}